package com.zhuss.food.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.user.UpdateUserBo;
import com.zhuss.bean.bo.food.order.wechat.CreateOrderBo;
import com.zhuss.bean.bo.food.order.wechat.CreateOrderProductBo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.processor.UserRead;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.EnumUtil;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.ProductType;
import com.zhuss.exception.WpsException;
import com.zhuss.food.db.dao.ext.OrderDao;
import com.zhuss.food.db.dao.ext.OrderProductDao;
import com.zhuss.food.db.entity.Order;
import com.zhuss.food.db.entity.OrderProduct;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.NoUtil;

/**
 * 创建订单 processor
 * @Author: zhushanshuo
 * @Date: 2020/03/08 16:01
 */

@Component("orderCreate")
public class OrderCreate extends AbstractProcessor<Object, CreateOrderBo> implements Processor<Object, CreateOrderBo> {
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private OrderProductDao orderProductDao;
	
	@Autowired
	@Qualifier("userRead")
	private UserRead userRead;
	
	@Autowired
	@Qualifier("userUpdate")
	private Processor<Object, UpdateUserBo> userUpdate;
	
	@Override
	public Object process(CreateOrderBo bo) throws WpsException {
		String phone = bo.getPhone();
		Order record = new Order();
		record.setAmount(bo.getAmount());
		record.setCount(bo.getCount());
		record.setOrderNo(NoUtil.ORDER + NoUtil.genOrder());
		record.setOrderSequence(NoUtil.simpleNo(record.getOrderNo()));
		record.setProductCount(bo.getProductCount());
		record.setStatus(bo.getStatus());
		record.setType(bo.getType());
		record.setDiningType(bo.getDiningType());
		record.setComment(bo.getComment());
		
		record.setDate(bo.getDate());
		record.setFoodTime(bo.getFoodTime());
		record.setPersonNum(bo.getPersonNum());
		record.setHavingStandard(bo.getHavingStandard());
		record.setDrinkType(bo.getDrinkType());
		
		
		record.setCreatorId(bo.getConcurrentUserId());
    	record.setCreator(bo.getConcurrentUserName());
    	record.setCreateTime(new Date());
		
		record.setArea(bo.getArea());
		record.setExpectFetchTimeStart(bo.getExpectFetchTimeStart());
		record.setExpectFetchTimeEnd(bo.getExpectFetchTimeEnd());
		
		orderDao.create(record);
		
		bo.setTargetId(record.getOrderId());
		
		List<CreateOrderProductBo> createOrderProductBoList =  bo.getCreateOrderProductBoList();
		for (CreateOrderProductBo cb : createOrderProductBoList) {
			OrderProduct op = new OrderProduct();
			op.setAmount(cb.getAmount());
			op.setCount(cb.getCount());
			op.setPrice(cb.getPrice());
			op.setProductName(cb.getProductName());
			op.setProductNo(cb.getProductNo());
			op.setProductId(cb.getProductId());
			op.setStatus(cb.getStatus());
			
			op.setOrderNo(record.getOrderNo());
			op.setOrderId(record.getOrderId());
			op.setCreateTime(record.getCreateTime());
			op.setCreator(record.getCreator());
			op.setCreatorId(record.getCreatorId());
			
			orderProductDao.create(op);
		}
		if (phone != null && !"".equals(phone.trim())) {
			DetailUserVo user = (DetailUserVo) userRead.detail(record.getCreatorId());
			user.setPhone(phone);
			UpdateUserBo updateUserBo = new UpdateUserBo();
			BeanUtils.copyProperties(bo, updateUserBo);
			BeanUtils.copyProperties(user, updateUserBo);
			userUpdate.process(updateUserBo);
		}
		return null;
	}

	@Override
	public Object log(CreateOrderBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.订单创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(EnumUtil.containsKey(ProductType.class, param.getType()));
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}

	@Override
	public void check(CreateOrderBo param) throws WpsException {
		// TODO Auto-generated method stub
		
	}

}
